/*
 * @Description:
 * @Author: yeke
 * @Date: 2024-03-28 20:36:39
 * @LastEditors: yeke
 * @LastEditTime: 2024-11-11 14:56:51
 */
import { HttpStatus, Injectable } from '@nestjs/common';
// import { CreateUploadDto } from './dto/create-upload.dto';
import { UpdateUploadDto } from './dto/update-upload.dto';
import { extname, join } from 'path';
import { InjectFluentFfmpeg, Ffmpeg } from '@mrkwskiti/fluent-ffmpeg-nestjs';

@Injectable()
export class UploadService {
  constructor(@InjectFluentFfmpeg() private readonly ffmpeg: Ffmpeg) {}
  async uploadFile(file: Express.Multer.File) {
    console.log('file', file);

    const outputPath = join(
      __dirname,
      `../../../public/video/out-${new Date().getTime()}${extname(
        file.originalname,
      )}`,
    );
    const outputImagePath = join(
      __dirname,
      `../../../public/video/out-${new Date().getTime()}.jpg`,
    );

    // ffmpeg -i input.mp4 -ss 00:00:01 -vframes 1 output.jpg

    // 压缩视频
    const ffmpegFun = () => {
      return new Promise((resolve, reject) => {
        this.ffmpeg(file.path)
          .output(outputPath)
          // .setFfmpegPath('D:/yeke/ffmpeg/bin/ffmpeg.exe') // 设置ffmpeg程序路径
          .output(outputPath) // 输出文件
          .on('压缩视频progress', (progress) => {
            // 监听切片进度
            console.log(progress);
            const ratio = Number(progress.percent).toFixed(2);
            console.log('压缩视频progress', ratio);
          })
          .on('end', () => {
            console.log('压缩视频end');
            resolve('end');
          })
          .on('error', (error) => {
            console.log('----压缩视频error', error);
            reject(error);
          })
          .run();
      });
    };

    // 获取第一帧作为图片
    const ffmpegGetImage = () => {
      return new Promise((resolve, reject) => {
        this.ffmpeg(file.path)
          .outputOptions([
            '-ss 00:00:01', // 设置开始时间
            '-vframes 1', // 设置截取一帧
          ])
          .output(outputImagePath)
          .on('progress', (progress) => {
            // 监听切片进度
            console.log(progress);
            const ratio = Number(progress.percent).toFixed(2);
            console.log('progress', ratio);
          })
          .on('end', () => {
            console.log('end');
            resolve('end');
          })
          .on('error', (error) => {
            console.log('----error', error);
            reject(error);
          })
          .run();
      });
    };

    try {
      await Promise.all([ffmpegFun(), ffmpegGetImage()]);
      return {
        code: HttpStatus.OK,
      };
    } catch (error) {
      return {
        code: HttpStatus.BAD_REQUEST,
        message: '失败',
      };
    }
  }

  findAll() {
    return `This action returns all upload`;
  }

  findOne(id: number) {
    return `This action returns a #${id} upload`;
  }

  update(id: number, updateUploadDto: UpdateUploadDto) {
    console.log(updateUploadDto);

    return `This action updates a #${id} upload`;
  }

  remove(id: number) {
    return `This action removes a #${id} upload`;
  }
}
